package com.tc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tc.dao.AdvertisingIMGMapper;
import com.tc.pojo.AdvertisingIMG;
import com.tc.service.AdvertisingIMGService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:AdvertisingIMG业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class AdvertisingIMGServiceImpl implements AdvertisingIMGService {

    @Autowired
    private AdvertisingIMGMapper advertisingIMGMapper;


    /**
     * AdvertisingIMG条件+分页查询
     * @param advertisingIMG 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<AdvertisingIMG> findPage(AdvertisingIMG advertisingIMG, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(advertisingIMG);
        //执行搜索
        return new PageInfo<AdvertisingIMG>(advertisingIMGMapper.selectByExample(example));
    }

    /**
     * AdvertisingIMG分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<AdvertisingIMG> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<AdvertisingIMG>(advertisingIMGMapper.selectAll());
    }

    /**
     * AdvertisingIMG条件查询
     * @param advertisingIMG
     * @return
     */
    @Override
    public List<AdvertisingIMG> findList(AdvertisingIMG advertisingIMG){
        //构建查询条件
        Example example = createExample(advertisingIMG);
        //根据构建的条件查询数据
        return advertisingIMGMapper.selectByExample(example);
    }


    /**
     * AdvertisingIMG构建查询对象
     * @param advertisingIMG
     * @return
     */
    public Example createExample(AdvertisingIMG advertisingIMG){
        Example example=new Example(AdvertisingIMG.class);
        Example.Criteria criteria = example.createCriteria();
        if(advertisingIMG!=null){
            // 编号
            if(!StringUtils.isEmpty(advertisingIMG.getId())){
                    criteria.andEqualTo("id",advertisingIMG.getId());
            }
            // 其他表关联的编号
            if(!StringUtils.isEmpty(advertisingIMG.getPid())){
                    criteria.andEqualTo("pid",advertisingIMG.getPid());
            }
            // 从图片表关联的编号
            if(!StringUtils.isEmpty(advertisingIMG.getSrc())){
                    criteria.andEqualTo("src",advertisingIMG.getSrc());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        advertisingIMGMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改AdvertisingIMG
     * @param advertisingIMG
     */
    @Override
    public void update(AdvertisingIMG advertisingIMG){
        advertisingIMGMapper.updateByPrimaryKey(advertisingIMG);
    }

    /**
     * 增加AdvertisingIMG
     * @param advertisingIMG
     */
    @Override
    public void add(AdvertisingIMG advertisingIMG){
        advertisingIMGMapper.insert(advertisingIMG);
    }

    /**
     * 根据ID查询AdvertisingIMG
     * @param id
     * @return
     */
    @Override
    public AdvertisingIMG findById(Integer id){
        return  advertisingIMGMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询AdvertisingIMG全部数据
     * @return
     */
    @Override
    public List<AdvertisingIMG> findAll() {
        return advertisingIMGMapper.selectAll();
    }
}
